using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace PSE.Hosting.Management.Windows
{
    public partial class AddServerForm : Form
    {
        public AddServerForm()
        {
            InitializeComponent();
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            if (!Business.ValidateServerName(txtServerName.Text))
            {
                MessageBox.Show("Invalid server name.", "Service Hoster", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (Business.GetServersFromConfigurationFile().Any(s => s.ToUpper() == txtServerName.Text.ToUpper()))
            {
                MessageBox.Show("Server name already exists.", "Service Hoster", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Business.AddServer(txtServerName.Text);
            this.Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
                
        private void AddServerForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }
    }
}